package com.clay.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clay.appointment.common.exception.ExceptionEnum;
import com.clay.appointment.common.exception.GlobalException;
import com.clay.appointment.entity.user.UserInfo;
import com.clay.appointment.utils.RequestContextUtils;
import com.clay.appointment.utils.TokenUtils;
import com.clay.appointment.vo.user.LoginVo;
import com.clay.user.mapper.UserInfoMapper;
import com.clay.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author : clay
 * @Description: 用户登录验证
 * @Date: Create in 15:44 2021/8/25
 * @Modified By: clay
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {


    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Map<String, Object> loginUser(LoginVo loginVo) {
        String phone = loginVo.getPhone();
        String code = loginVo.getCode();
        //校验参数
        if (StringUtils.isBlank(phone) ||
                StringUtils.isBlank(code)) {
            GlobalException.throwOut(ExceptionEnum.PARAMETER_ERROR);
        }

//        判断手机验证码是否为空
//        String indentifyCode = redisTemplate.opsForValue().get(phone).toString();
//        if(!code.equals(indentifyCode))
//        {
//            GlobalException.throwOut(ExceptionEnum.CODE_ERROR);
//        }

        //手机号已被使用
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);

        //获取会员
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);

        //没有此会员，新建会员
        if (userInfo == null) {
            userInfo = new UserInfo();
            userInfo.setName(phone);
            userInfo.setPhone(phone);
            userInfo.setStatus(1);
            userInfoMapper.insert(userInfo);
             userInfo = baseMapper.selectOne(queryWrapper);
        }

        //校验是否被禁用
        if (userInfo.getStatus() == 0) {
            GlobalException.throwOut(ExceptionEnum.LOGIN_DISABLED_ERROR);
        }

        //TODO 记录登录


        //返回页面显示名称
        Map<String, String> map = new HashMap<>(2);

        String userId = userInfo.getId().toString();

        String name = userInfo.getName();

        if (StringUtils.isBlank(name)) {
            name = userInfo.getNickName();
        }
        String ipAddr = RequestContextUtils.getRemoteAddr();

        map.put("ip", ipAddr);

        map.put("userId", userId);

        String token = TokenUtils.encode(map);

        loginVo.setIp(ipAddr);
        loginVo.setOpenid(userId);
        redisTemplate.opsForValue().set("token",loginVo,30, TimeUnit.MINUTES);
        log.info("token值"+token);
        
        Map<String, Object> map1 = new HashMap<>(2);
        map1.put("name", name);
        map1.put("token", token);

        return map1;
    }
}
